package org.fireflow2.designer.eclipse.outline;

import org.eclipse.gef.EditDomain;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.gef.ui.parts.SelectionSynchronizer;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.IPageSite;
import org.eclipse.ui.views.contentoutline.ContentOutline;
import org.fireflow2.designer.eclipse.actions.DesignPanelContextMenuProvider;
import org.fireflow2.designer.eclipse.editors.WorkflowProcessDesignerPanel;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.parts.TreePartFactory;
import org.fireflow2.designer.eclipse.simulation.SimulatorPanel;

/**
 * 大纲页面，该页面显示当前的subprocess的结构，而不是显示完整的WorkflowProcess的结构
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class WorkflowProcessContentOutlinePage 
	extends ContentOutlinePage implements	ISelectionListener{
    private Control outline;
    private WorkflowProcessDesignerPanel processEditor;
    private SimulatorPanel simulator ;
    private SubProcessWrapper subflowWrapper;
    

    public WorkflowProcessContentOutlinePage(WorkflowProcessDesignerPanel processEditor,
    		SimulatorPanel simulator,
    		SubProcessWrapper subflowWrapper) {
        super(new TreeViewer());        
        this.processEditor = processEditor;
        this.simulator = simulator;
        this.subflowWrapper = subflowWrapper;
    }
    
    
	public void init(IPageSite pageSite) {
		super.init(pageSite);
		pageSite.getWorkbenchWindow().getSelectionService().addSelectionListener(this);

		//设置工具栏上的action
		ActionRegistry registry = getActionRegistry(this.processEditor);
		IActionBars bars = pageSite.getActionBars();

		String id = IWorkbenchActionConstants.DELETE;
		bars.setGlobalActionHandler(id, registry.getAction(id));

      
		id = IWorkbenchActionConstants.UNDO;
		bars.setGlobalActionHandler(id, registry.getAction(id));

      
		id = IWorkbenchActionConstants.REDO;
		bars.setGlobalActionHandler(id, registry.getAction(id));
      
		bars.updateActionBars();
		
		//设置右键菜单
		DesignPanelContextMenuProvider contextMenuProvider = new DesignPanelContextMenuProvider(this.getViewer(),registry);
		this.getViewer().setContextMenu(contextMenuProvider);
		
		
		
	}
	
    public void createControl(Composite parent) {
        outline = getViewer().createControl(parent);
        getViewer().setEditDomain(getEditDomain(this.processEditor));  
        
        getSelectionSynchronizer(this.processEditor).addViewer(getViewer());
        getSelectionSynchronizer(this.simulator).addViewer(getViewer());
        
        
        getViewer().setEditPartFactory(new TreePartFactory());
        getViewer().setContents(subflowWrapper);           
    }

    public Control getControl() {
        //            return pageBook;

        return outline;
    }

    public void dispose() {
    	SelectionSynchronizer synchronizer = getSelectionSynchronizer(this.processEditor);
    	if (synchronizer!=null){
    		synchronizer.removeViewer(getViewer());
    	}
    	
    	synchronizer = getSelectionSynchronizer(this.simulator);
    	if (synchronizer!=null){
    		synchronizer.removeViewer(getViewer());
    	}    	
        super.dispose();
    }    
    
    private SelectionSynchronizer getSelectionSynchronizer(GraphicalEditor graphicalEditor){
    	SelectionSynchronizer synchronizer = (SelectionSynchronizer)graphicalEditor.getAdapter(SelectionSynchronizer.class);
    	return synchronizer;
    }
    
    private EditDomain getEditDomain(GraphicalEditor graphicalEditor){
    	EditDomain editDomain = (EditDomain)graphicalEditor.getAdapter(EditDomain.class);
    	return editDomain;
    }
    
    private ActionRegistry getActionRegistry(GraphicalEditor graphicalEditor){
    	ActionRegistry actionRegistry = (ActionRegistry)graphicalEditor.getAdapter(ActionRegistry.class);
    	return actionRegistry;
    }

	public void selectionChanged(IWorkbenchPart arg0, ISelection arg1) {
		// TODO Auto-generated method stub
		ContentOutline outline = null;
		if (arg0 instanceof ContentOutline){
			outline = (ContentOutline)arg0;
			if (this.equals(outline.getCurrentPage())){
//				processEditor.updateSelectionActions();
			}
		}
		
	}
}

